/*
 * CFileWriter.cpp
 *
 *  Created on: 04.12.2012
 *      Author: caffeine
 */

#include "CFileWriter.h"
#include <lampe/exception/IO.h>

namespace lampe{
namespace io{
u32 CFileWriter::position(){
	return ftell(pointer);
}

void CFileWriter::position(u32 position){
	fseek(pointer,position,SEEK_SET);
}

u32 CFileWriter::size(){
	s32 pos=ftell(pointer);
	fseek(pointer,0,SEEK_END);
	u32 ret=ftell(pointer);
	fseek(pointer,pos,SEEK_SET);
	return ret;
}

u32 CFileWriter::write(void* buffer, u32 size){
	return fwrite(buffer,size,1,pointer);
}

CFileWriter::CFileWriter(boost::filesystem::path path) {
	pointer=fopen(path.string().c_str(),"wb");
	if (!pointer)
		GEN_IO_ERROR("File "+String(path.string().c_str())+" can't be open or create!");
}

CFileWriter::~CFileWriter() {
	fclose(pointer);
}

extern IFileWriter*createFileWriter(boost::filesystem::path fileName){
	return new CFileWriter(fileName);
}

extern void writeTextToFile(boost::filesystem::path fileName,String text){
	IFileWriter*file=createFileWriter(fileName);
	file->write((void*)&text.c_str()[0],text.length());
	file->drop();
}

}//namespace io
}//namespace lampe
